import 'package:flutter/material.dart';
import 'package:flutter_09_game/base/controller/base_list_more_controller.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/http/http_export.dart';
import 'package:flutter_09_game/pages/deposit/models/model_record_team_commission.dart';
import 'package:flutter_09_game/pages/deposit/utils_deposit.dart';
import 'package:flutter_09_game/user/app_user_manager.dart';
import 'package:flutter_date_pickers/flutter_date_pickers.dart';
import 'package:get/get.dart';

class DepositRecordTeamCommissionLogic extends BaseListMoreController {
  TextEditingController idController = TextEditingController();

  TextEditingController idRegController = TextEditingController();
  RxString timeRx = LocaleKeys.today.tr.obs;

  RxString symbolRx = ''.obs;

  late RxString statusRx = statusRows.first.obs;

  List<String> statusRows = [
    LocaleKeys.allStatus.tr,
    LocaleKeys.unsettled.tr,
    LocaleKeys.settling.tr,
    LocaleKeys.settled.tr,
  ];

  // 日期范围相关属性
  Rx<DatePeriod?> selectedDateRange = Rx<DatePeriod?>(null);

  @override
  Future<List> loadData() async {
    final dateRange = UtilsDeposit.calculateDateRange(timeRx.value,
        selectedDateRange: selectedDateRange.value);

    var params = {
      'is_team': true,
      'page': pageNum,
      'size': pageSize,
      'symbol': symbolRx.value,
      'start': dateRange['start'],
      'end': dateRange['end'],
      'user_identity': idController.text.trim(),
      'identity': idRegController.text.trim(),
      'status': statusRows.indexOf(statusRx.value)
      // ?identity=&user_identity=&status=0&symbol=&is_team=true&start=2025-09-16+00:00:00&end=2025-09-16+23:59:59&page=1&size=10
    };

    // getUserTeamStatistics(params);
    HttpResponse response = await HttpManager().get(API.getGameTeamRoundJoin,
        params: params, fromJson: ModelRecordTeamCommission.fromJson);
    // [log] {is_team: true, page: 1, size: 20, symbol: , start: 2025-11-06 00:00:00, end: 2025-11-06 23:59:59, user_identity: , identity: , status: 0}

    // ?only_bet=false&start=2025-09-15+00:00:00&end=2025-09-15+23:59:59&symbol=TRX&user_identity=&size=10&page=1
    return response.data ?? [];
  }
  /// 更新时间选择
  void updateTimeSelection(String timeType) {
    if (timeType == LocaleKeys.custom.tr) {
      UtilsDeposit.showDateRangePicker(selectedDateRange.value).then((value) {
        if (value is DatePeriod) {
          selectedDateRange.value = value;
          refreshController.requestRefresh();
          timeRx.value = timeType;
        }
      });
    } else {
      timeRx.value = timeType;
      // 清除自定义日期范围
      selectedDateRange.value = null;
      refreshController.requestRefresh();
    }
  }

}
